<template>
  <div :id="id" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
    data: {
      type: Object,
      default: function () {
       return {}
      },
    },
    title: {
      type: String,
      default: "",
    },
    type:{
      type: String,
      default: "分",
    }
  },
  data() {
    return {
      chart: null,
      legendData: [],
      echartData: [],
      resizeTimer: null,
    };
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.erchartResize);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart = null;
  },
  methods: {
    erchartResize() {
      if (this.resizeTimer) clearTimeout(this.resizeTimer);
      this.resizeTimer = setTimeout(() => {
        this.chart.resize();
      }, 100);
    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      const option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        legend: {
          show: false,
        },
        toolbox: {},
        grid: {
          left: "5%",
          right: "5%",
          top: "10%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: true,
            data: this.data.questionArr,
            axisLabel: {
              show: true,
              textStyle: {
                color: "#333",
                fontSize: 10,
              },
              rotate: 30,
              interval: 0,
            },
          },
        ],
        yAxis: {
          type: "value",
          min: 0,
          max: 100,
          axisLabel: {
            formatter: "{value}分",
          },
        },
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 1,
            end: 8,
            height: 20,
            bottom: 5,
            textStyle: { color: "#333" },
          },
        ],
        series: [
          {
            name: "平均分",
            type: "line",
            itemStyle: {
              normal: {
                //颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
                color: "#3398DB", //背景渐变色
                lineStyle: {
                  // 系列级个性化折线样式
                  width: 2,
                  type: "solid",
                  color: "#3398DB",
                },
              },
            }, //线条样式
            emphasis: {
              itemStyle: {
                shadowColor: "rgba(255, 255, 255, 0.8)",
                shadowBlur: 15,
              },
            },
            data: this.data.scoreArr,
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
  destroyed() {
    window.removeEventListener("resize", this.erchartResize);
  },
};
</script>
<style lang='stylus' scoped>
.chartsClass {
  padding-left: 1.2rem;
}
</style>
</script>